

***************************
* Lead Items (12') Pooled *
***************************

* DME claims
{
use ".\statadata\dme_total_matched", clear
gen lead = .
replace lead = 1 if hcpcs_cd=="B4035" // Enteral Nutrient
replace lead = 1 if hcpcs_cd=="E0260" & mod1=="RR" // Hospital bed
replace lead = 1 if hcpcs_cd=="E0277" & mod1=="RR" // Mattress
replace lead = 1 if hcpcs_cd=="E0163" & mod1=="NU" // Commode chair
replace lead = 1 if hcpcs_cd=="E0630" & mod1=="RR" // Patient lift
replace lead = 1 if hcpcs_cd=="A4595" // TENS
replace lead = 1 if hcpcs_cd=="E0143" & mod1=="NU" // Walker
replace lead = 1 if hcpcs_cd=="E2402" & mod1=="RR" // NPWT
replace lead = 1 if hcpcs_cd=="E1390" & mod1=="RR" // Oxygen concentrator
replace lead = 1 if hcpcs_cd=="E0601" & mod1=="RR" // CPAP
replace lead = 1 if hcpcs_cd=="E0570" & mod1=="RR" // Nebulizer
replace lead = 1 if hcpcs_cd=="E0784" & mod1=="RR" // Infusion pumps
keep if lead==1
gen yq = qofd(clm_thru_dt)
format yq %tq
collapse (count) n_claim = line_alowd_chrg_amt (sum) total_amt = line_alowd_chrg_amt q = line_srvc_cnt, by(bene_id hcpcs_cd mod1 mod2 category_x r1_x r2_x r1rc_x yq)
save ".\temp\sample_lead.dta", replace	
}

* Number of Claim in 2010 as weights
{
preserve
gen year = year(dofq(yq))
keep if year==2010
merge m:1 bene_id year using ".\temp\mbsf", keepusing(geo_id) keep(3) nogen
collapse (sum) payment_2010=total_amt claim_2010=n_claim, by(geo_id hcpcs_cd mod1 mod2)
save ".\temp\claim_weight_lead", replace
restore
}

* MBSF: Enrollment info
{
keep bene_id
duplicates drop
merge 1:m bene_id using ".\temp\mbsf", keep(3) nogen
keep if year>=2009
keep bene_id year geo_id cba_round enroll* dual* 
reshape long enroll_Q dual_Q, i(bene_id year geo_id cba_round) j(qtr)
gen yq = yq(year,qtr)
format yq %tq

keep if enroll_Q==3 
gen dual = 1 if dual_Q==3
replace dual = 0 if dual_Q==0
bysort bene_id: egen temp = count(enroll_Q)
gen enroll_28 = 1 if temp==28 
keep bene_id yq geo_id cba_round dual enroll_28

merge 1:m bene_id yq using ".\temp\sample_lead.dta", keep(3) nogen
save ".\temp\sample_lead.dta", replace
}


*** Supplier ***
{
use ".\statadata\dme_total_matched", clear
gen lead = .
replace lead = 1 if hcpcs_cd=="B4035" // Enteral Nutrient
replace lead = 1 if hcpcs_cd=="E0260" & mod1=="RR" // Hospital bed
replace lead = 1 if hcpcs_cd=="E0277" & mod1=="RR" // Mattress
replace lead = 1 if hcpcs_cd=="E0163" & mod1=="NU" // Commode chair
replace lead = 1 if hcpcs_cd=="E0630" & mod1=="RR" // Patient lift
replace lead = 1 if hcpcs_cd=="A4595" // TENS
replace lead = 1 if hcpcs_cd=="E0143" & mod1=="NU" // Walker
replace lead = 1 if hcpcs_cd=="E2402" & mod1=="RR" // NPWT
replace lead = 1 if hcpcs_cd=="E1390" & mod1=="RR" // Oxygen concentrator
replace lead = 1 if hcpcs_cd=="E0601" & mod1=="RR" // CPAP
replace lead = 1 if hcpcs_cd=="E0570" & mod1=="RR" // Nebulizer
replace lead = 1 if hcpcs_cd=="E0784" & mod1=="RR" // Infusion pumps
keep if lead==1

gen yq = qofd(clm_thru_dt)
format yq %tq
gen year = year(dofq(yq))
merge m:1 bene_id year using ".\temp\mbsf", keepusing(geo_id) keep(1 3) nogen

collapse (count) n_claim = line_alowd_chrg_amt (sum) total_amt = line_alowd_chrg_amt, by(prvdr_npi yq geo_id hcpcs_cd mod1 mod2)
bysort geo_id yq hcpcs_cd mod1 mod2: egen total_total_amt = sum(total_amt)
gen share = total_amt/total_total_amt
gen share2 = share^2
gen n = 1 if share>0.01

preserve
collapse (sum) N_NPI = n HHI_NPI = share2, by(geo_id yq hcpcs_cd mod1 mod2)
drop if geo_id==""
drop if yq==.
save ".\temp\sample_lead_supplier", replace
restore

collapse (sum) n_claim total_amt, by(prvdr_npi yq geo_id)
bysort geo_id yq: egen total_total_amt = sum(total_amt)
gen share = total_amt/total_total_amt
gen share2 = share^2
collapse (sum) HHI_NPI2 = share2, by(geo_id yq)
merge 1:m geo_id yq using ".\temp\sample_lead_supplier", nogen
save ".\temp\sample_lead_supplier", replace

}


*** Regression Sample Construction ***
{
use ".\temp\sample_lead.dta", clear

gen n_bene = 1
bysort bene_id hcpcs_cd mod1 mod2: egen temp = min(yq)
gen n_initiator = 1 if temp==yq
drop temp

gen n_bene2 = 1 if enroll_28==1
gen n_initiator2 = n_initiator if enroll_28==1
gen q2 = q if enroll_28==1
gen total_amt2 = total_amt if enroll_28==1
gen n_claim2 = n_claim if enroll_28==1

collapse (sum) n_bene* n_initiator* q* total_amt* n_claim*,	by(hcpcs_cd mod1 mod2 category_x r1_x r2_x r1rc_x yq geo_id cba_round)
drop if yq==.
drop if geo_id==""
unique hcpcs_cd mod1 mod2
unique yq
unique geo_id
di 969*28*12
gen product = hcpcs_cd+"-"+mod1+"-"+mod2
egen product_id = group(product)
save ".\temp\sample_lead_reg.dta", replace

preserve
keep product_id hcpcs_cd mod1 mod2 category_x r1_x r2_x r1rc_x
duplicates drop
tempfile temp
save `temp'
restore
keep geo_id cba_round
duplicates drop
expand 28
bysort geo_id: gen yq = yq(2008,4)+_n
format yq %tq
expand 12
bysort geo_id yq: gen product_id = _n
merge 1:1 geo_id yq product_id using ".\temp\sample_lead_reg.dta", nogen
drop hcpcs_cd mod1 mod2 category_x r1_x r2_x r1rc_x
merge m:1 product_id using `temp', nogen
merge 1:1 geo_id yq hcpcs_cd mod1 mod2 using ".\temp\sample_lead_supplier.dta", nogen
gen year = year(dofq(yq))
merge m:1 geo_id yq using ".\temp\population", keep(1 3) nogen
merge m:1 geo_id hcpcs_cd mod1 mod2 using ".\temp\claim_weight_lead", keep(1 3) nogen
replace payment_2010 = 0 if payment_2010==.
replace claim_2010 = 0 if claim_2010==.

replace q = 0 if q==.
replace n_bene = 0 if n_bene==.
replace n_initiator = 0 if n_initiator==.
replace total_amt = 0 if total_amt==.

gen qX = q/pop_FFS*1000
gen NbeneX = n_bene/pop_FFS*1000
gen NinitiatorX = n_initiator/pop_FFS*1000
gen paymentX = total_amt/pop_FFS*1000

gen lg_qX = log(qX)
gen lg_NbeneX = log(NbeneX)	
gen lg_NinitiatorX = log(NinitiatorX)	
gen lg_paymentX = log(paymentX)

gen price = total_amt/q
gen lg_price = log(price)

replace N_NPI = 0 if N_NPI==.
gen N_NPIX = N_NPI/pop_FFS*1000
foreach v of varlist N_NPI* HHI* {
	replace `v' = . if q==0
}

gen dd = 0
replace dd = 1 if yq>=yq(2011,1) & yq<=yq(2013,4) & cba_round==1 & r1_x==1
replace dd = 0 if yq>=yq(2011,1) & yq<=yq(2013,4) & geo_id!="CBA57" & inlist(hcpcs_cd, "E0193", "E0277", "E0371", "E0372", "E0373") // Mattress, Miami only
replace dd = 1 if yq>=yq(2013,3) & yq<=yq(2016,2) & cba_round==2 & r2_x==1
replace dd = 1 if yq>=yq(2014,1) & yq<=yq(2016,4) & cba_round==1 & r1rc_x==1
save ".\temp\sample_lead_reg.dta", replace
}


*** Regression: DID Main effect (Lead) ***
use ".\temp\sample_lead_reg.dta", clear
egen geo_idnum = group(geo_id)

* Main
{
local rp replace
foreach ver in price qX NbeneX NinitiatorX paymentX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010], nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in N_NPI HHI_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010], nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA3_DIDLead") sheetreplace	
restore
}


* Main (population weights)
{
local rp replace
foreach ver in price qX NbeneX NinitiatorX paymentX {
	cap n qui reghdfe lg_`ver' dd [aweight=pop_FFS_2010], nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=pop_FFS_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in N_NPI HHI_NPI {
	cap n qui reghdfe `ver' dd [aweight=pop_FFS_2010], nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=pop_FFS_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA4_DIDLead_popw") sheetreplace	
restore
}


* By wave
{
local rp replace
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==1) & r1_x==1 & category!="Mattress", nocons absorb(geo_idnum#product_id yq#product_id) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R1, Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}

foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==1) & r1_x==1 & yq<yq(2014,1) & category!="Mattress", nocons absorb(geo_idnum#product_id yq#product_id) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R1_bf2014, Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==2) & r2_x==1, nocons absorb(geo_idnum#product_id yq#product_id) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R2, Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==1) & r1rc_x==1 & r1_x==., nocons absorb(geo_idnum#product_id yq#product_id) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R1RC, Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA6_DIDLead_Wave") sheetreplace	
restore

local rp replace
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==1) & r1_x==1 & category!="Mattress", nocons absorb(geo_idnum#product_id yq#product_id) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R1, Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==1) & r1_x==1 & yq<yq(2014,1) & category!="Mattress", nocons absorb(geo_idnum#product_id yq#product_id) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R1_bf2014, Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==2) & r2_x==1, nocons absorb(geo_idnum#product_id yq#product_id) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R2, Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==1) & r1rc_x==1, nocons absorb(geo_idnum#product_id yq#product_id) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R2, Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA10_DIDLead_Wave_HHI") sheetreplace	
restore
}

* Geographic spillover
{
use ".\raw\ZIP_CBA_CBSA.dta", clear
keep geo_id CBA_adjacent 
duplicates drop
drop if geo_id==""

merge 1:m geo_id using ".\temp\sample_lead_reg.dta", keep(2 3) nogen
egen geo_idnum = group(geo_id)

local rp replace
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if CBA_adjacent==., nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if CBA_adjacent==., nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}

foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if CBA_adjacent!=. | cba_round!=0, nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if CBA_adjacent!=. | cba_round!=0, nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}

replace dd = 1 if yq>=yq(2011,1) & yq<=yq(2013,4) & CBA_adjacent==1 & r1_x==1
replace dd = 0 if yq>=yq(2011,1) & yq<=yq(2013,4) & inlist(geo_id,"17500","28580","33100","34940","36380","38940")==0 & inlist(hcpcs_cd, "E0193", "E0277", "E0371", "E0372", "E0373") // Mattress, Miami only
replace dd = 1 if yq>=yq(2013,3) & yq<=yq(2016,2) & CBA_adjacent==2 & r2_x==1
replace dd = 1 if yq>=yq(2014,1) & yq<=yq(2016,4) & CBA_adjacent==1 & r1rc_x==1

foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if cba_round==0, nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if cba_round==0, nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA12_DIDLead_Adjacent") sheetreplace	
restore
}

* Robustness
{
use ".\temp\sample_lead_reg.dta", clear
egen geo_idnum = group(geo_id)

* Robustness 1: no non-CBA
local rp replace
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd if cba_round!=0 [aweight=claim_2010], nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Robust, NoNonCBA, Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}

* Robustness 2: no small non-CBA
preserve 
use ".\temp\ZIP_CBA_CBSA.dta", clear
keep geo_id insample 
duplicates drop 
tempfile temp 
save `temp'
restore
merge m:1 geo_id using `temp', keep(3) nogen
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd if insample==1 [aweight=claim_2010], nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
		qui sum `ver' if e(sample) [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Robust, BigNonCBA, Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean))
}

* Robustness 3: State FEs
preserve
use ".\temp\ZIP_CBA_CBSA.dta", clear
keep geo_id CBA state state_name cba_round
drop if state=="" | geo_id==""
drop if geo_id=="CBA15" & state_name=="ME" // CBA = Boston, MA-NH only 1 zipcode
duplicates drop
gen R1 = 1 if cba_round==1
gen R2 = 1 if cba_round==2
bysort state: egen R1X = max(R1)
bysort state: egen R2X = max(R2)
drop R1 R2
expand 28
bysort geo_id state: gen yq = yq(2008,4)+_n
format yq %tq
levelsof state, local(levels)
foreach l of local levels {
	gen state_`l' = (state=="`l'")
	gen state_`l'_post = 0
	replace state_`l'_post = 1 if state=="`l'" & R1X==1 & yq>=yq(2011,1)
	replace state_`l'_post = 1 if state=="`l'" & R2X==1 & yq>=yq(2013,3)
}
drop state_name
collapse (max) state_*, by(geo_id yq)
tempfile temp_state
save `temp_state'
restore
merge m:1 geo_id yq using `temp_state', nogen
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd state_*_post [aweight=claim_2010], nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta keep(dd) addtext(Robust, StateFE, Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, State FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean))
}

* Robustness 4: 28-quarter Enrollees
replace q2 = 0 if q2==.
gen q2X = q2/pop_FFS_full28Q*1000
gen lg_q2X = log(q2X)

replace n_bene2 = 0 if n_bene2==.
gen n_bene2X = n_bene2/pop_FFS_full28Q*1000
gen lg_n_bene2X = log(n_bene2X)

replace n_initiator2 = 0 if n_initiator2==.
gen Ninitiator2X = n_initiator2/pop_FFS_full28Q*1000
gen lg_Ninitiator2X = log(Ninitiator2X)	

replace total_amt2 = 0 if total_amt2==.
gen payment2X = total_amt2/pop_FFS_full28Q*1000
gen lg_payment2X = log(payment2X)

gen price2 = total_amt2/q2
gen lg_price2 = log(price2)

foreach ver in price2 q2X {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010], nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
		qui sum `ver' [aweight=claim_2010]
		cap n outreg2 using $resdir\reg.dta, dta addtext(Robust, Enrollee28, Product FE, X, Yr-Qtr FE, X, GEO FE, X, Product-YQ FE, X, Product-GEO FE, X, GEO-YQ FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean))
}

preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA5_DIDLead_robust") sheetreplace	
restore

* Robustness 5: 12 separate DID
cap gen desc = ""
replace desc = "Enteral Nutrient" if hcpcs_cd=="B4035" // Enteral Nutrient
replace desc = "Hospital bed" if hcpcs_cd=="E0260" // Hospital bed
replace desc = "Support Surface" if hcpcs_cd=="E0277" // Mattress
replace desc = "Commode chair" if hcpcs_cd=="E0163" // Commode chair
replace desc = "Patient lift" if hcpcs_cd=="E0630" // Patient lift
replace desc = "TENS" if hcpcs_cd=="A4595" // TENS
replace desc = "Walker" if hcpcs_cd=="E0143" // Walker
replace desc = "NPWT" if hcpcs_cd=="E2402" // NPWT
replace desc = "Oxygen concentrator" if hcpcs_cd=="E1390" // Oxygen concentrator
replace desc = "CPAP" if hcpcs_cd=="E0601" // CPAP
replace desc = "Nebulizer" if hcpcs_cd=="E0570" // Nebulizer
replace desc = "External Infusion" if hcpcs_cd=="E0784" // External Infusion
levelsof desc, local(levels)
foreach ver in price qX {
    local rp replace
	foreach l of local levels {
		cap n qui reghdfe lg_`ver' dd if desc=="`l'" [aweight=pop_FFS_2010], nocons absorb(geo_idnum yq) vce(cluster geo_id)
			qui sum `ver' if e(sample) [aweight=pop_FFS_2010]
			cap n outreg2 using $resdir\reg.dta, dta addtext(Yr-Qtr FE, X, GEO FE, X, Product, "`l'", SE cluster, geo, weights, pop_2010) addstat(Mean of Dep Var, r(mean)) `rp'
		local rp
	}
	preserve
	use $resdir\reg_dta.dta, clear
	export excel using $resdir\Table.xls, sheet("TableA7_DIDLead12_`ver'") sheetreplace	
	restore
}

foreach ver in price qX {
    local rp replace
	foreach l of local levels {
		cap n qui reghdfe `ver' dd if desc=="`l'" [aweight=pop_FFS_2010], nocons absorb(geo_idnum yq) vce(cluster geo_id)
			qui sum `ver' if e(sample) [aweight=pop_FFS_2010]
			cap n outreg2 using $resdir\reg.dta, dta addtext(Yr-Qtr FE, X, GEO FE, X, Product, "`l'", SE cluster, geo, weights, pop_2010) addstat(Mean of Dep Var, r(mean)) `rp'
		local rp
	}
	preserve
	use $resdir\reg_dta.dta, clear
	export excel using $resdir\Table.xls, sheet("TableA7RR_DIDLead12_`ver'_l") sheetreplace	
	restore
}

}


*** Event Study Plot (Lead) ***
{
use ".\temp\sample_lead_reg.dta", clear
gen CBP = .
replace CBP = yq(2011,1) if cba_round==1 & r1_x==1
replace CBP = . if geo_id!="CBA57" & inlist(hcpcs_cd, "E0193", "E0277", "E0371", "E0372", "E0373") // Mattress, Miami only
replace CBP = yq(2013,3) if cba_round==2 & r2_x==1 & CBP==.
replace CBP = yq(2014,1) if cba_round==1 & r1rc_x==1 & CBP==.
gen relative_qtr = yq-CBP
tab relative_qtr, m
drop if relative_qtr>9 & relative_qtr!=.
forvalues i = -20/9 {
	local q = `i'+21
	gen Q`q' = (relative_qtr==`i')
}
egen geo_idnum = group(geo_id)
local qtr_list Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13 Q14 Q15 Q16 Q17 Q18 Q19 /// Q20 as baseline
	 Q21 Q22 Q23 Q24 Q25 Q26 Q27 Q28 Q29 Q30 

foreach ver in price qX NbeneX NinitiatorX paymentX {
	if "`ver'"=="price" {
		local t "Log Price"
	}
	if "`ver'"=="qX" {
		local t "Log Quantity"
	}
	if "`ver'"=="NbeneX" {
		local t "Log Beneficiaries"
	}
	if "`ver'"=="NinitiatorX" {
		local t "Log New Beneficiaries"
	}
	if "`ver'"=="paymentX" {
		local t "Log Spending"
	}

	qui reghdfe lg_`ver' `qtr_list' [aweight=claim_2010], nocons absorb(geo_idnum#product_id yq#product_id geo_idnum#yq) vce(cluster geo_id)
	outreg2 using $resdir\reg.dta, dta fmt(f) stat(coef se ci_low ci_high) noparen noaster replace
	
	preserve
	use $resdir\reg_dta.dta, clear
	gen i = 1 if v1!=""
	gen n = sum(i)
	bysort n: gen t = _n
	drop i
	reshape wide v1 v2, i(n) j(t)
	rename v11 var
	rename v21 coef
	rename v22 se
	rename v23 ci_low
	rename v24 ci_high
	keep var coef se ci_low ci_high
	gen relative_qtr = substr(var,2,.)
	keep relative_qtr coef se ci_low ci_high
	destring relative_qtr coef se ci_low ci_high, replace force
	replace relative_qtr = relative_qtr-21
	drop if relative_qtr==.
	set obs 30
	replace relative_qtr = -1 if relative_qtr==.
	foreach s in se coef ci_low ci_high {
		replace `s' = 0 if relative_qtr==-1
	}
	tostring relative_qtr, gen(qtr)
	label define relative_qtr_v -20 "-20" -16 "-16" -12 "-12" -8 "-8" -4 "-4" 0 "0" 4 "4" 8 "8", replace
	label value relative_qtr relative_qtr_v
	sort relative_qtr
	scatter ci_high ci_low coef relative_qtr, connect(direct direct direct) ///
		lc(black black black) ms(none none none) lp(dot dot solid) ///
		xline(0, lwidth(vthin) lp(dash)) xlabel(-20(4)8, valuelabel) legend(off) ///
		xtitle("Relative Qtr to CBP") title("`t'") graphregion(color(white))
	graph export $resdir\Fig1_`ver'_lead.png, as(png) replace
	graph export $resdir\Fig1_`ver'_lead.pdf, as(pdf) replace
	restore
}

}



*********************
*** Dual Analysis ***
*********************

* Sample Construction 
{
use ".\temp\sample_lead.dta", clear

bysort bene_id geo_id: egen temp = min(yq)
cap gen n_initiator = 1 if temp==yq
gen n_bene = 1 if q!=.

collapse (count) N_bene = q (sum) n_initiator q total_amt n_claim, ///
	by(hcpcs_cd mod1 mod2 category_x r1_x r2_x r1rc_x y yq geo_id cba_round dual)
drop if dual==.
drop if yq==.
drop if geo_id==""
unique yq
unique geo_id
di 969*28*2*12
gen product = hcpcs_cd+"-"+mod1+"-"+mod2
egen product_id = group(product)
tempfile temp
save `temp'

preserve
keep product_id hcpcs_cd mod1 mod2 category_x r1_x r2_x r1rc_x
duplicates drop
tempfile temp2
save `temp2'
restore
keep geo_id cba_round
duplicates drop
expand 28
bysort geo_id: gen yq = yq(2008,4)+_n
format yq %tq
expand 12
bysort geo_id yq: gen product_id = _n
expand 2
bysort geo_id yq product_id: gen dual = _n-1
merge 1:1 geo_id yq product_id dual using `temp', nogen
drop hcpcs_cd mod1 mod2 category_x r1_x r2_x r1rc_x
merge m:1 product_id using `temp2', nogen
merge m:1 geo_id yq using ".\temp\population", keep(1 3) nogen
merge m:1 geo_id hcpcs_cd mod1 mod2 using ".\temp\claim_weight_lead", keep(1 3) nogen
replace payment_2010 = 0 if payment_2010==.
replace claim_2010 = 0 if claim_2010==.

replace q = 0 if q==.
rename N_bene n_bene
replace n_bene = 0 if n_bene==.
replace n_initiator = 0 if n_initiator==.
replace total_amt = 0 if total_amt==.


gen qX = q/pop_FFSnondual*1000 if dual==0
replace qX = q/pop_FFSdual*1000 if dual==1
gen NbeneX = n_bene/pop_FFSnondual*1000 if dual==0
replace NbeneX = n_bene/pop_FFSdual*1000 if dual==1
gen NinitiatorX = n_initiator/pop_FFSnondual*1000 if dual==0
replace NinitiatorX = n_initiator/pop_FFSdual*1000 if dual==1
gen paymentX = total_amt/pop_FFSnondual*1000 if dual==0
replace paymentX = total_amt/pop_FFSdual*1000 if dual==1
	
gen lg_qX = log(qX)
gen lg_NbeneX = log(NbeneX)	
gen lg_NinitiatorX = log(NinitiatorX)	
gen lg_paymentX = log(paymentX)

gen price = total_amt/q
gen lg_price = log(price)

gen dd = 0
replace dd = 1 if yq>=yq(2011,1) & yq<=yq(2013,4) & cba_round==1 & r1_x==1
replace dd = 0 if yq>=yq(2011,1) & yq<=yq(2013,4) & geo_id!="CBA57" & inlist(hcpcs_cd, "E0193", "E0277", "E0371", "E0372", "E0373") // Mattress, Miami only
replace dd = 1 if yq>=yq(2013,3) & yq<=yq(2016,2) & cba_round==2 & r2_x==1
replace dd = 1 if yq>=yq(2014,1) & yq<=yq(2016,4) & cba_round==1 & r1rc_x==1
egen geo_idnum = group(geo_id)

cap gen nondual = (dual==0)
cap gen CBA_nondual = (cba_round>0 & nondual==1)
cap gen dd_nondual = dd*nondual
cap gen OOPC = 0.2*price if dual==0
replace OOPC = 0 if dual==1 & price!=.
}

* Regressions
{
*** Table: DDD (Pooled) ***
egen geo_product = group(geo_id product_id)
egen yq_product = group(yq product_id)
egen geo_yq = group(geo_id yq)

qui reghdfe OOPC dd_nondual dd [aweight=claim_2010], nocons absorb(geo_product#dual yq_product#dual geo_yq#dual) vce(cluster geo_id) 
qui sum OOPC if e(sample) & year==2010 & dual==0 [aweight=claim_2010]
outreg2 using $resdir\reg.dta, dta addtext(Geo-Product-Dual FE, X, YQ-Product-Dual FE, X, Geo-YQ-Dual FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var (Non-dual Pre), r(mean)) replace
foreach ver in qX NbeneX NinitiatorX {
	qui reghdfe lg_`ver' dd_nondual dd [aweight=claim_2010], nocons absorb(geo_product#dual yq_product#dual geo_yq#dual) vce(cluster geo_id)
	qui sum `ver' if e(sample) & year==2010 & dual==0 [aweight=claim_2010]
	outreg2 using $resdir\reg.dta, dta addtext(Geo-Product-Dual FE, X, YQ-Product-Dual FE, X, Geo-YQ-Dual FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var (Non-dual Pre), r(mean)) 
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA21_DualOLS_lead") sheetreplace	
restore

*** Table: IV (Pooled) ***
local rp replace
foreach ver in qX NbeneX NinitiatorX {
	qui ivreghdfe lg_`ver' (OOPC = dd_nondual) dd [aweight=claim_2010], nocons absorb(geo_product#dual yq_product#dual geo_yq#dual) cluster(geo_id)
	qui sum OOPC if e(sample) & year==2010 & dual==0 [aweight=claim_2010]
	local oopc_mean = r(mean)
	local elasticity = _b[OOPC]*`oopc_mean'
	local fstat = e(rkf)
	outreg2 using $resdir\reg.dta, dta addtext(Geo-Product-Dual FE, X, YQ-Product-Dual FE, X, Geo-YQ-Dual FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var (Non-dual Pre), `oopc_mean', Elasiticity, `elasticity', F stat, `fstat') `rp'
	local rp
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA21_DualIV_lead") sheetreplace	
restore

}

* State Medigap
{
preserve
use ".\statadata\mbsf", clear
keep if year==2015
gen ZIP = substr(zip_cd,1,5)
drop zip_cd
merge m:1 ZIP using ".\raw\ZIP_CBA_CBSA.dta", keep(1 3) nogen

drop if state=="" | geo_id==""
drop if bene_hmo_cvrage_tot_mons>0
keep geo_id CBA state state_name cba_round
gen n_pop = 1 
collapse (sum) n_pop, by(geo_id state_name)
tempfile temp_state
save `temp_state'

import excel "S:\group\mgduggan\CBP\raw\Medigap.xlsx", sheet("Sheet1") firstrow clear
rename State state_name
merge 1:m state_name using `temp_state', keep(3) nogen
collapse (mean) Share [aw=n_pop], by(geo_id)

sum Share, de
xtile share_2 = Share, nq(2)
xtile share_3 = Share, nq(3)
xtile share_4 = Share, nq(4)
replace share_4 = 2 if share_4==3 
replace share_4 = 3 if share_4==4
tempfile temp_state
save `temp_state'
restore
merge m:1 geo_id using `temp_state', nogen

cap egen geo_product = group(geo_id product_id)
cap egen yq_product = group(yq product_id)
cap egen geo_yq = group(geo_id yq)

local rp replace
forvalues i = 1/2 {
	qui reghdfe lg_qX dd_nondual dd [aweight=claim_2010] if share_2==`i', nocons absorb(geo_product#dual yq_product#dual geo_yq#dual) vce(cluster geo_id)
	qui sum qX if e(sample) & year==2010 & dual==0 [aweight=claim_2010]
	outreg2 using $resdir\reg1.dta, dta keep(dd_nondual dd) addtext(Geo-Product-Dual FE, X, YQ-Product-Dual FE, X, Geo-YQ-Dual FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var (Non-dual Pre), r(mean))  `rp'

	qui ivreghdfe lg_qX (OOPC = dd_nondual) dd [aweight=claim_2010] if share_2==`i', nocons absorb(geo_product#dual yq_product#dual geo_yq#dual) cluster(geo_id)
	qui sum OOPC if e(sample) & year==2010 & dual==0 [aweight=claim_2010]
	local oopc_mean = r(mean)
	local elasticity = _b[OOPC]*`oopc_mean'
	qui sum Share if e(sample) [aweight=claim_2010]
	local Medigap_share = r(mean)
	local fstat = e(rkf)
	outreg2 using $resdir\reg2.dta, keep(OOPC dd) dta addtext(Geo-Product-Dual FE, X, YQ-Product-Dual FE, X, Geo-YQ-Dual FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var (Non-dual Pre), `oopc_mean', Elasiticity, `elasticity', Medigap Share, `Medigap_share', Medigap Median, `i', F stat, `fstat') `rp'
	local rp
}
forvalues i = 1/3 {
	qui reghdfe lg_qX dd_nondual dd [aweight=claim_2010] if share_3==`i', nocons absorb(geo_product#dual yq_product#dual geo_yq#dual) vce(cluster geo_id)
	qui sum qX if e(sample) & year==2010 & dual==0 [aweight=claim_2010]
	outreg2 using $resdir\reg1.dta, dta keep(dd_nondual dd) addtext(Geo-Product-Dual FE, X, YQ-Product-Dual FE, X, Geo-YQ-Dual FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var (Non-dual Pre), r(mean)) 

	qui ivreghdfe lg_qX (OOPC = dd_nondual) dd [aweight=claim_2010] if share_3==`i', nocons absorb(geo_product#dual yq_product#dual geo_yq#dual) cluster(geo_id)
	qui sum OOPC if e(sample) & year==2010 & dual==0 [aweight=claim_2010]
	local oopc_mean = r(mean)
	local elasticity = _b[OOPC]*`oopc_mean'
	qui sum Share if e(sample) [aweight=claim_2010]
	local Medigap_share = r(mean)
	local fstat = e(rkf)
	outreg2 using $resdir\reg2.dta, keep(OOPC dd) dta addtext(Geo-Product-Dual FE, X, YQ-Product-Dual FE, X, Geo-YQ-Dual FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var (Non-dual Pre), `oopc_mean', Elasiticity, `elasticity', Medigap Share, `Medigap_share', Medigap Tercile, `i', F stat, `fstat')
}
preserve
use $resdir\reg1_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA21RR_DualOLS_lead_medigap") sheetreplace	
use $resdir\reg2_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA21RR_DualIV_lead_medigap") sheetreplace	
restore
}

